#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>

void Swap(int* a, int* b)
{
	int tmp = *a;
	*a = *b;
	*b = tmp;
}

void Print(int* arr, int n)
{
	for (int i = 0;i < n;i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

void SelectSort(int* arr, int n)
{
	int begin = 0, end = n - 1;
	while (begin < end)
	{
		int maxi = begin, mini = begin;
		for (int i = begin + 1;i <= end;i++)
		{
			if (arr[i] < arr[mini])
			{
				mini = i;
			}
			if (arr[i] > arr[maxi])
			{
				maxi = i;
			}
		}
		if (begin == maxi)
		{
			Swap(&arr[begin], &arr[mini]);
			Swap(&arr[end], &arr[mini]);
		}
		else
		{
			Swap(&arr[begin], &arr[mini]);
			Swap(&arr[end], &arr[maxi]);
		}
		begin++;
		end--;
	}
}

int main()
{
	int arr[] = { 9,8,5,3,7,1,4,6,0 };
	int n = sizeof(arr) / sizeof(arr[0]);
	printf("排序前：");
	Print(arr, n);
	SelectSort(arr, n);
	printf("排序后: ");
	Print(arr, n);
	return 0;
}